using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using FiltronASP.Helpers.HttpObjects;

namespace FiltronASP.Helpers.FileUpload
{
    public class ThumbnailerHelper
    {
        /// <summary>
        /// Metoda do tworzenia miniaturek.
        /// </summary>
        /// <param name="image">obrazek, z ktorego robimy miniaturke</param>
        /// <param name="newWidth">nowa szerokosc, gdy nullem, to skalowanie wg wysokosci</param>
        /// <param name="newHeight">nowa wysokosc, gdy nullem, to skalowanie wg szerokosci</param>
        /// <returns>true, gdy sie uda, false wpp</returns>
        public bool MakeThumb(string imagePath, int? newWidth, int? newHeight)
        {
            try
            {
                if ((newWidth == null) && (newHeight == null))
                {
                    return false;
                }   //if
                HttpServerUtility httpServer = HttpObjectsHelper.GetHttpServer();
                if (httpServer == null)
                {
                    Logger.MyLogger.Log("ThumbnailerHelper exception: MakeThumb method, httpServer null object...");
                    return false;
                }   //if
                if (!File.Exists(httpServer.MapPath(imagePath)))
                {
                    Logger.MyLogger.Log("ThumbnailerHelper exception: MakeThumb method, image file does not exist...");
                    return false;
                }   //if
                Image image = Image.FromFile(httpServer.MapPath(imagePath));
                if (image != null)
                {
                    string thumbDestination = this.GetThumbDestination(imagePath, newWidth, newHeight);
                    if (!String.IsNullOrEmpty(thumbDestination))
                    {
                        if (newWidth == null)
                        {
                            newWidth = (int)(image.Width * ((float)newHeight / (float)image.Height));
                        }   //if
                        if (newHeight == null)
                        {
                            newHeight = (int)(image.Height * ((float)newWidth / (float)image.Width));
                        }   //if
                        if ((newHeight != null) && (newWidth != null)) 
                        {
                            Image thumbnailImage = image.GetThumbnailImage(newWidth.Value, newHeight.Value, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                            if (thumbnailImage != null)
                            {                        
                                thumbnailImage.Save(thumbDestination);
                                return true;
                            }   //if
                        }   //if
                    }   //if
                }   //if not null
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return false;            
        }

        /// <summary>
        /// Metoda zwracajaca katalog, gdzie trzymana miniaturka.
        /// </summary>
        /// <param name="imagePath">obrazek, z ktorego robimy miniaturke</param>
        /// <param name="newWidth">nowa szerokosc, gdy nullem, to skalowanie wg wysokosci</param>
        /// <param name="newHeight">nowa wysokosc, gdy nullem, to skalowanie wg szerokosci</param>
        /// <returns>true, gdy sie uda, false wpp</returns>
        public string GetThumbDestination(string imagePath, int? newWidth, int? newHeight)
        {
            try
            {
                if ((newWidth == null) && (newHeight == null))
                {
                    return String.Empty;
                }   //if
                HttpServerUtility httpServer = HttpObjectsHelper.GetHttpServer();
                if (httpServer != null)
                {
                    string thumbDirectory = String.Empty;
                    if (newWidth == null)
                    {
                        thumbDirectory = Path.Combine(
                                httpServer.MapPath(UploadConsts.UPLOADED_IMAGE_ABSOLUTE_DIR), UploadConsts.THUMBS_DIR_NULL_WIDTH_PREFIX + newHeight.Value.ToString()
                            );
                        if (!Directory.Exists(thumbDirectory))
                        {
                            Directory.CreateDirectory(thumbDirectory);
                        }   //if
                        if (!Directory.Exists(thumbDirectory)) {
                            return String.Empty;
                        }   //if
                        return Path.Combine( thumbDirectory, Path.GetFileName(imagePath));
                    }   //if
                    if (newHeight == null)
                    {
                        thumbDirectory = Path.Combine(
                                httpServer.MapPath(UploadConsts.UPLOADED_IMAGE_ABSOLUTE_DIR), UploadConsts.THUMBS_DIR_NULL_HEIGHT_PREFIX + newWidth.Value.ToString()
                            );
                        if (!Directory.Exists(thumbDirectory))
                        {
                            Directory.CreateDirectory(thumbDirectory);
                        }   //if
                        if (!Directory.Exists(thumbDirectory))
                        {
                            return String.Empty;
                        }   //if
                        return Path.Combine(thumbDirectory, Path.GetFileName(imagePath));
                    }   //if
                    thumbDirectory = Path.Combine(
                            httpServer.MapPath(UploadConsts.UPLOADED_IMAGE_ABSOLUTE_DIR), newWidth.Value.ToString() + UploadConsts.THUMBS_DIR_WIDTH_HEIGHT_PREFIX + newHeight.Value.ToString()
                        );
                    if (!Directory.Exists(thumbDirectory))
                    {
                        Directory.CreateDirectory(thumbDirectory);
                    }   //if
                    if (!Directory.Exists(thumbDirectory))
                    {
                        return String.Empty;
                    }   //if
                    return Path.Combine(thumbDirectory, Path.GetFileName(imagePath));
                }   //if not null
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

        /// <summary>
        /// Callback przy tworzeniu miniaturki, na razie nieuzywane.
        /// </summary>
        /// <returns>zawse true</returns>
        public bool ThumbnailCallback()
        {
            return true;
        }

    }
}
